package edu.neusoft.yuejian_09.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.activity.MasterAskActivity;
import edu.neusoft.yuejian_09.adapter.MasterQuestionAdapter;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Question;
import edu.neusoft.yuejian_09.model.MasterModel;

public class MasterQuesFragment extends Fragment implements RetrofitListener<List<Question>> {

    private RecyclerView recyclerView;
    private List mDataList;
    Button btn_ask;

    private View view=null;
    private int master_id;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view= inflater.inflate(R.layout.master_ques_fragment, container, false);

        initViews();
        initEvents();
        getQalist();
        return view;
    }

    private void initViews(){

        recyclerView= (RecyclerView)view.findViewById(R.id.rv_masterquestion);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        btn_ask=(Button)view.findViewById(R.id.ask);

    }
    private void initEvents(){
        btn_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MasterQuesFragment.this.getActivity(), MasterAskActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putInt("master_id",master_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    void getQalist(){
        Bundle bundle = getActivity().getIntent().getExtras();
        master_id = bundle.getInt("master_id");
        System.out.printf("marster_id");
        MasterModel masterModel=new MasterModel();
        masterModel.Qalist(master_id,this);
    }

    @Override
    public void onSuccess(List<Question> data,int flag ){

        recyclerView.setAdapter(new MasterQuestionAdapter(MasterQuesFragment.this.getActivity(),data));
    }

    @Override
    public void onFail(){ Toast.makeText(getActivity(), "网络发生错误", Toast.LENGTH_SHORT).show();}
}
